using System;
using System.Collections;
using System.Collections.Generic;
using Edge.Common;
using Edge.Common.ComponentModel.Notification;
using Edge.Common.Services;
using Edge.Common.Utils;
using Edge.DAL.Common;
using Edge.DAL.Common.ResultSet;
using Edge.DAL.Managers;
using NHibernate;
using NHibernate.Expression;
using NHibernate.Type;
using Shop.Business.Domain.Catalog;
using Shop.Business.Domain.Catalog.Parameter;
using Shop.Business.Domain.Customers;
using System.Linq;

namespace Shop.DAL.Managers
{
    public class ParameterValueManager : AbstractManager<ParameterValue, int, Customer>, IParameterValueManager
    {
       // private ResultSetTransformer<ParameterValue> parameterValueTransformer;

        public ParameterValueManager()
        {
            #region ParameterValue mapper

//            ResultSetMapper<ParameterValue> pvt = new ResultSetMapper<ParameterValue>();
//            pvt.AddMap("Id", "Id");
//            pvt.AddMap("ParametrId", "Parameter");
//            pvt.AddMap("Value", "Value");
//            parameterValueTransformer = new ResultSetTransformer<ParameterValue>(pvt);

            #endregion
        }

        public ICollection<ParameterValue> GetProductPatameterValues(int prodId, int paremeterId)
        {
            using (IDalOperation op = CreateLoadOperation())
            {
                IQuery query = Session.GetNamedQuery("GetProductPatameterValues");
                query.SetInt32("parameterID", paremeterId);
                query.SetInt32("productID", prodId);
                ICollection<ParameterValue> ret = query.List<ParameterValue>();
                op.Commit();
                return ret;
            }
        }

        public ICollection<ParameterValue> GetGroupPatameterValues(int groupId, int paremeterId)
        {
            using (IDalOperation op = CreateLoadOperation())
            {
                IQuery query = Session.GetNamedQuery("GetGroupPatameterValues");
                query.SetInt32("parameterID", paremeterId);
                query.SetInt32("groupID", groupId);
                ICollection<ParameterValue> ret = query.List<ParameterValue>();
                op.Commit();
                return ret;
            }
        }

        public ICollection<ParameterValue> GetGroupPatameterValuesOnly(int groupId, int paremeterId)
        {
            using (IDalOperation op = CreateLoadOperation())
            {
                IQuery query = Session.GetNamedQuery("GetGroupPatameterValuesOnly");
                query.SetInt32("parameterID", paremeterId);
                query.SetInt32("groupID", groupId);
                ICollection<ParameterValue> ret = query.List<ParameterValue>();
                op.Commit();
                return ret;
            }
        }

        public ICollection<ParameterValue> Find(FetchOptions fetchOptions, ParameterValueFilter filter)
        {
            ICriteria criteria = CreateCriteria(fetchOptions);
            AddRestrictions(criteria, filter);
            return criteria.List<ParameterValue>();
        }

        public int Count(ParameterValueFilter filter)
        {
            ICriteria criteria = CreateCriteria();
            AddRestrictions(criteria, filter);
            criteria.SetProjection(Projections.RowCount());
            return (int)criteria.List()[0];
        }

        private void AddRestrictions(ICriteria criteria, ParameterValueFilter filter)
        {
            if (!StringUtils.IsBlankOrNull(filter.GetText()) && !(filter.GetText().Trim().Equals("")))
            {
                criteria.Add(Expression.Disjunction()
                                 .Add(Expression.Like("Value", filter.GetText().Trim()))
                    );
            }
            if (filter.Parameter != null)
                criteria.Add(Expression.Eq("Parameter", filter.Parameter));
            if (filter.AtMenu)
            {
                criteria.Add(Expression.And(Expression.IsNotNull("OrderOf"), Expression.Not(Expression.Eq("OrderOf",0))));
            }
        }

        public void SetProductPatameterValues(int prodID, int valueID)
        {
            using (IDalOperation op = CreateLoadOperation())
            {
                IQuery query = Session.GetNamedQuery("SetProductPatameterValues");
                query.SetParameter("productID", prodID, TypeFactory.Basic("int"));
                query.SetParameter("parameterValueID", valueID, TypeFactory.Basic("int"));
                query.UniqueResult();
                op.Commit();
            }
        }

        public void SetProductPatameterValues(Product product, int valueID)
        {
            SetProductPatameterValues(product.Id, valueID);
            //SetGroupPatameterValues(product.Group.Id, valueID);
        }



        public void SetGroupPatameterValues(int groupID, int valueID)
        {
            using (IDalOperation op = CreateLoadOperation())
            {
                IQuery query = Session.GetNamedQuery("SetGroupPatameterValues");
                query.SetParameter("groupID", groupID, TypeFactory.Basic("int"));
                query.SetParameter("parameterValueID", valueID, TypeFactory.Basic("int"));
                query.UniqueResult();
                op.Commit();
            }
        }
    }

    public class SizeValueManager : AbstractManager<SizeValue, int, Customer>, ISizeValueManager
    {

        public ICollection<SizeValue> Find(FetchOptions fetchOptions, SizeValueFilter filter)
        {
            ICriteria criteria = CreateCriteria(fetchOptions);
            AddRestrictions(criteria, filter);
            return criteria.List<SizeValue>();
        }

        public int Count(SizeValueFilter filter)
        {
            ICriteria criteria = CreateCriteria();
            AddRestrictions(criteria, filter);
            criteria.SetProjection(Projections.RowCount());
            return (int)criteria.List()[0];
        }

        private void AddRestrictions(ICriteria criteria, SizeValueFilter filter)
        {
            if (filter.Group != null)
                criteria.Add(Expression.Eq("Group", filter.Group));
           
        }

       
    }
}